package com.innovation.chengzhangriji.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.ui.widget.CuteToolbar;
import com.innovation.chengzhangriji.ui.widget.CuteTwoButton;

public class SelectActivity extends BaseActivity implements CuteToolbar.OnClickCuteToolbarListener, View.OnClickListener {

    private int mState = -1;

    private CuteTwoButton mButtonNew;
    private CuteTwoButton mButtonScan;
    private CuteTwoButton mButtonSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupLayout();
    }

    @Override
    public void onBackPressed() {
        onClickLeft();
    }

    @Override
    public void onClickLeft() {
        finish();
    }

    @Override
    public void onClickRight() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_new:
                switchState(0);
                break;
            case R.id.button_scan:
                switchState(1);
                break;
            case R.id.button_search:
                switchState(2);
                break;
        }
    }

    private void setupLayout() {
        setContentView(R.layout.activity_select);
        CuteToolbar cuteToolbar = (CuteToolbar) findViewById(R.id.toolbar);
        cuteToolbar.setOnClickCuteToolbarListener(this);
        mButtonNew = (CuteTwoButton) findViewById(R.id.button_new);
        mButtonScan = (CuteTwoButton) findViewById(R.id.button_scan);
        mButtonSearch = (CuteTwoButton) findViewById(R.id.button_search);
        mButtonNew.setOnClickListener(this);
        mButtonScan.setOnClickListener(this);
        mButtonSearch.setOnClickListener(this);
        switchState(0);
    }

    private void switchState(int state) {
        if (mState == state) {
            return;
        }
        mState = state;
        mButtonNew.setOneState();
        mButtonScan.setOneState();
        mButtonSearch.setOneState();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment fragment = null;
        switch (state) {
            case 0:
                mButtonNew.setTwoState();
                fragment = new Select1Fragment();
                break;
            case 1:
                mButtonScan.setTwoState();
                fragment = new Select2Fragment();
                break;
            case 2:
                mButtonSearch.setTwoState();
                fragment = new Select3Fragment();
                break;
        }
        ft.replace(R.id.content, fragment);
        ft.commit();

    }
}
